package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Petinfo;
import com.pojo.Petmaster;
import com.pojo.RoleUser;
import com.pojo.User;
import com.service.impl.UserServiceimpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Zhengjz
 * @version 1.0
 * @className UserController
 * @date 2021/3/1 12:16
 */
@Controller
public class UserController {

    @Autowired
    private UserServiceimpl userServiceimpl;



    //跳转登录页面
    @RequestMapping("/tologin")
    public String toLogin(){
        return "index";
    }

    //登录请求处理
    @PostMapping("/login")
    public String login(User user, HttpServletRequest request){
        System.out.println(user);
        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        //获取令牌
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(), user.getPassword());
            try {

                subject.login(token);//执行登录
                request.getSession().setAttribute("user",user);
                return "menu/main";
            } catch (UnknownAccountException e) {
                request.setAttribute("message", "用户名或密码错误！");
                return "index";
            } catch (IncorrectCredentialsException e) {
                request.setAttribute("message", "用户名或密码错误！");
                return "index";
            }
    }


    //用户信息展示
    @RequestMapping("/userinfolist")
    public String userInfo(HttpServletRequest request,Integer pageNum){
        if (pageNum == null) {
            pageNum = 1;
        }
        String name = request.getParameter("name");
        if (name == null || name == "") {
            //当前第几页，每页显示5条数据
            PageHelper.startPage(pageNum, 5);
            List<Petmaster> petmaster = userServiceimpl.findPetmaster();
            PageInfo pageTeacher = new PageInfo<>(petmaster);
            request.setAttribute("list", pageTeacher);
        }else{
            request.setAttribute("name",name);
            //当前第几页，每页显示5条数据
            PageHelper.startPage(pageNum, 5);
            List<Petmaster> byName = userServiceimpl.selectPetmaster(name);
            PageInfo pageTeacher =  new PageInfo<>(byName);
            request.setAttribute("list",pageTeacher);
        }
        return "user/userlist";
    }


    //注册页面
    @RequestMapping("/zhuce")
    public String zhuce(){
        return "user/regist";
    }

    //用户注册
    @PostMapping("/regist")
    public String regist(User user, Petmaster petmaster,Model model){
        String url = "";
        System.out.println("user:"+user+","+"petmaster："+petmaster);
        Boolean name = userServiceimpl.findName(user.getUserName());
        System.out.println("name："+name);
        if(name){
            model.addAttribute("message","用户名已存在！");
            url = "user/regist";
        }else{
            //写入用户名和密码
            user.setDelFlag(0); //0 代表正常
            user.setStatis(0);
            userServiceimpl.addUser(user);
            //写入个人基本信息
            User user1 = userServiceimpl.selById(user.getUserName());
            petmaster.setUserId(user1.getId());
            userServiceimpl.addPetmaster(petmaster);
            //指定该用户的角色
        RoleUser roleUser = new RoleUser();
        roleUser.setRoleId(3);
        roleUser.setUserId(user1.getId());
        userServiceimpl.addroleId(roleUser);
        url = "index";
    }
        return url;
    }


    @RequestMapping("/delpetuser")
    public void delUser(String id, HttpServletResponse response) throws IOException {
        System.out.println("id："+id);
        int i = userServiceimpl.delPetUser(id);
        response.getWriter().println(i);
    }


    @RequestMapping("/selrole")
    public void selRole(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //获取当前登录用户的id
        User user = (User) request.getSession().getAttribute("user");
        User byId = userServiceimpl.findById(user.getUserName());
        //拿到当前登录用户的id去查询对应的角色
        RoleUser roleUser = userServiceimpl.findRoleUser(Integer.toString(byId.getId()));
       // System.out.println(roleUser.getRoleId());
        response.getWriter().println(roleUser.getRoleId());
    }

}
